home *** CD-ROM | disk | FTP | other *** search
/ IRIS Performer 2.2 Friends Demo / SGI IRIS Performer 2.2 Friends Demo.iso / friends / devices / BGSystems / CerealBox / g_test.c < prev    next >
C/C++ Source or Header  |  1997-10-31  |  4KB  |  242 lines

  1. /*
  2.  * g_test.c
  3.  *
  4.  * Main routine for v3.0 test for LV824-3G
  5.  *
  6.  * Copyright 1995 BG Systems 
  7.  *
  8.  * Author       Date       Comments
  9.  * John Green   15-Jan-95  Written
  10.  * John Green   01-Feb-95  Clean up for release of 3.0
  11.  *
  12.  *
  13.  */
  14.  
  15. static char SccsId[] = "@(#)g_test.c    1.3 10 Feb 1995";
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <math.h>
  21. #include <signal.h>
  22.  
  23. #include <sys/schedctl.h>
  24. #include <sys/types.h>
  25. #include <sys/time.h>
  26.  
  27. #include "lv3.h"
  28.  
  29. void setup_lv(void);
  30. void init_timer(void);
  31. void rf_quit(void);
  32. void catcher(void);
  33. int  ms_time(double *);
  34.  
  35. extern int open_lv(bglv *, char *, int );
  36. extern int init_lv(bglv *);
  37. extern int close_lv(bglv * );
  38.  
  39. extern int w_lv(int, char *);
  40. extern int r_lv(bglv *);
  41. extern int send_outputs(bglv *);
  42. extern int check_inputs(bglv *);
  43.  
  44. extern int check_setup( bglv *);
  45. extern void pattern(long *, bglv *);
  46.  
  47. bglv bgdata;
  48.  
  49. double start, now;
  50. struct itimerval itv;
  51.  
  52. /*
  53.  * main()
  54.  *
  55.  * This is where everything happens
  56.  *
  57.  */
  58.  
  59. main()
  60. {
  61.    int      st;
  62.    long     counter = 0;
  63.    long     pc;
  64.    int      i;
  65.    int      j;
  66.    char     inputs = 1;
  67.    char     outputs = 1;
  68.    float    si;
  69.  
  70.  
  71.    setup_lv();
  72. #ifndef DEBUG
  73.    init_timer();
  74. #endif
  75.  
  76.    st = ms_time(&start);
  77.  
  78.    i = 0;
  79.    pc = 0;
  80.    while(counter++ < 100000)
  81.    {
  82. /*
  83.  *  Set analog output
  84.  */
  85.       si = sin( counter/100.0 );     
  86.       bgdata.aout[0] = (int) ((1.0+si)*2048);
  87.       bgdata.aout[1] = 4095-bgdata.aout[0];
  88.       bgdata.aout[2] = counter/15;
  89.  
  90. /*
  91.  *  Set digital output
  92.  */
  93.       pattern(&pc, &bgdata);
  94.  
  95.       if ( outputs )
  96.          send_outputs(&bgdata);
  97.  
  98.       st = ms_time(&now);
  99. #ifdef DEBUG
  100.       sginap(3);
  101. #else
  102.       sigpause(SIGALRM);
  103. #endif
  104.  
  105.       st = check_inputs(&bgdata);
  106.                   
  107.       if ( inputs && counter % 15 == 0)
  108.       {
  109.          for ( i = 0; i < 8; i++ )
  110.          {
  111.             if ( bgdata.analog_in & 0x1 << i )
  112.                printf("%4.2f  ", bgdata.ain[i] );
  113.          }
  114.          for ( i = 0; i < 3; i++ )
  115.          {
  116.             if ( bgdata.analog_out & 0x1 << i )
  117.                printf("%d  ", bgdata.aout[i] );
  118.          }
  119.          for ( j = 0; j <=2; j++ )
  120.          {
  121.             if ( bgdata.dig_in & 0x10 << j )
  122.             {
  123.                for ( i = 0; i < 8; i++ )
  124.                {
  125.                   if ( (bgdata.din[j]>>i) & 0x1 )
  126.                      printf("1");
  127.                   else
  128.                      printf("0");
  129.                }
  130.                printf("  ");
  131.             }
  132.          }
  133.  
  134.          printf("\n");
  135.       }
  136.       if ( (counter % 200) == 0 )
  137.          printf("%7ld  Update rate = %5.2f Hz\n",
  138.                   counter, counter/(now-start) );
  139.    }
  140.  
  141.    printf("%ld transfers in %f sec.\n", counter, (now-start) );
  142.    printf("Update rate = %5.2f Hz\n", counter/(now-start) );
  143. }
  144.  
  145.  
  146. void setup_lv()
  147. {
  148.    int st;
  149.    int i;
  150.    
  151. /*
  152.  * Set to 1 analog input, and 8 discretes
  153.  */
  154.  
  155.    bgdata.analog_in = 0;
  156.    bgdata.analog_in = AIC1;
  157.  
  158.    bgdata.dig_in = DIC1;
  159.  
  160. /*
  161.  *  For outputs set all 3 analogs, and the top 16 discretes
  162.  */
  163.    bgdata.analog_out = AOC1 | AOC2 | AOC3;
  164.  
  165.    bgdata.dig_out = DOC2 | DOC3;
  166.    
  167.    bgdata.dout[0] = 0x0;
  168.    bgdata.dout[1] = 0x0;
  169.    bgdata.dout[2] = 0x0;
  170.  
  171. /*
  172.  *  Set the baud rate
  173.  */
  174.    bgdata.baud    = BAUD192;
  175.  
  176. /*
  177.  *  Open the port & drivers
  178.  */
  179.    st = open_lv(&bgdata, "/dev/ttyd2", FB_NOBLOCK);
  180.    if (st < 0)
  181.    {
  182.       printf("Unable to open port\n");
  183.       exit(-1);
  184.    }
  185.  
  186.    if ( bgdata.Rev.major < 3 )
  187.    {
  188.       printf("This test program requires a Rev 3.x EPROM\n");
  189.       exit(-1);
  190.    }
  191.  
  192. /*
  193.  *  Send the init string
  194.  */
  195.    st = init_lv(&bgdata);
  196.    if ( st < 0 )
  197.    {
  198.       printf("Invalid setup requested.  Bye\n");
  199.       exit(-1);
  200.    }
  201. }
  202.  
  203. void catcher()
  204. {
  205. }
  206.  
  207. int ms_time(double *t)
  208. {
  209.    struct timeval tp;
  210.    struct timezone tzp;
  211.    int st;
  212.  
  213.    st = gettimeofday(&tp, &tzp);
  214.    *t = tp.tv_sec +  ((float)tp.tv_usec / 1000000.0);
  215. }
  216.  
  217. void rf_quit()
  218. {
  219.    int st;
  220.  
  221.    st = close_lv(&bgdata);
  222.    printf("Bye\n");
  223.    exit(0);
  224. }
  225.  
  226.  
  227. void init_timer()
  228. {
  229. /*
  230.  * Initialize the itimer
  231.  */
  232.    itv.it_interval.tv_sec = 0;
  233.    itv.it_interval.tv_usec = 50000;
  234.    itv.it_value = itv.it_interval;
  235.    setitimer(ITIMER_REAL, &itv, (struct itimerval *)0 );
  236.  
  237.    sigset(SIGALRM,  catcher);
  238.    sigset(SIGQUIT,  rf_quit);
  239.    sigset(SIGKILL,  rf_quit);
  240.    sigset(SIGINT,   rf_quit);
  241. }
  242.